﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace FirstLimitUpMode.Views
{
    /// <summary>
    /// LocationCheckView.xaml 的交互逻辑
    /// </summary>
    public partial class LocationCheckView : Window
    {
        private List<Window> windows = new List<Window>();
        public static string filePath = "LocationData.txt";
        public static int MaxNum = 3;
        public LocationCheckView()
        {
            InitializeComponent();
        }

        private void ShowLocationBtn_Click(object sender, RoutedEventArgs e)
        {
            WindowsClear();
            var pts = GetLocations();
            if(pts.Count == MaxNum)
            {
                for (int i = 0; i < MaxNum; i++)
                {
                    var view = PointView.NewInstance(i + 1);
                    view.Left = pts[i].X - 15;
                    view.Top = pts[i].Y - 15;
                    view.Show();
                    windows.Add(view);
                }
            }
        }
        public static List<Point> GetLocations()
        {
            List<Point> points = new List<Point>();
            if (File.Exists(filePath))
            {
                var str = File.ReadAllText(filePath);
                if (!string.IsNullOrEmpty(str))
                {
                    var strs = str.Split(';');
                    if (strs.Count() != MaxNum)
                        return points;
                    for (int i = 0; i < MaxNum; i++)
                    {
                        var xys = strs[i].Split(',');
                        points.Add(new Point(double.Parse(xys[0]) + 15, double.Parse(xys[1]) + 15));
                    }
                }
            }
            return points;
        }
        private void WindowsClear()
        {
            foreach (var item in windows)
            {
                item.Close();
            }
            windows.Clear();
        }

        private void SetLocationBtn_Click(object sender, RoutedEventArgs e)
        {
            if(windows.Count == MaxNum)
            {
                if(MessageBox.Show("是否保存当前定位","提示",MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (var item in windows)
                    {
                        sb.Append(item.Left).Append(",").Append(item.Top).Append(";");
                    }
                    File.WriteAllText(filePath, sb.ToString().TrimEnd(';'));
                    MessageBox.Show("保存完成");
                    WindowsClear();
                    this.Close();
                    return;
                }
            }
            var view = PointView.NewInstance(windows.Count + 1);
            var point = this.PointToScreen(new Point(240,20));
            view.Left = point.X - 15;
            view.Top = point.Y - 15;
            view.Show();
            windows.Add(view);

            if(windows.Count == MaxNum)
            {
                SetLocationBtn.Content = "确定";
            }
        }

        private void ClearBtn_Click(object sender, RoutedEventArgs e)
        {
            WindowsClear();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            WindowsClear();
        }
    }
}
